package com.atguigu.controller;

import com.atguigu.commonutils.R;
import com.atguigu.service.impl.CrmBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.atguigu.educms.entity.CrmBanner;
/**
 * @author cyt
 * @create 2020-05-14 11:26
 */
@Api(description = "后台轮播图管理")
@RequestMapping("/educms/banner")
@RestController
public class EduAdminBannerController {
    /**
     * 后台的增删改查
     */
    @Autowired
    CrmBannerService service;

    /**
     * 添加首页轮播图
     * @param crmBanner
     * @return
     */
    @PostMapping("/saveBanner")
    public R getBanner(@RequestBody CrmBanner crmBanner) {
        service.save(crmBanner);
        return R.ok();
    }

    /**
     * 查询首页轮播图
     * @param id
     * @return
     */
    @GetMapping("/saveBanner/{id}")
    public R saveBanner(@PathVariable("id") String id) {
        CrmBanner banner = service.getById(id);
        return R.ok().data("banner", banner);
    }

    /**
     * 删除首页轮播图
     * @param id
     * @return
     */
    @DeleteMapping("/deleteBanner/{id}")
    public R deleteBanner(@PathVariable("id") String id) {
        boolean b = service.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 分页首页轮播图
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/getPageBanner/{page}/{limit}")
    public R getPageBanner(@PathVariable("page") String page, @PathVariable("limit") String limit) {
        Page<CrmBanner> bannerPage = new Page<CrmBanner>(Integer.parseInt(page), Integer.parseInt(limit));
        return R.ok().data("total", bannerPage.getTotal()).data("data", bannerPage.getRecords());
    }

}
